﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using automobili_data;
using automobili_data.interfaces;

namespace automobili_gui.Ulogovan_korisnik
{
    public partial class PregledSopOglasa : System.Web.UI.Page
    {
        
       
        
       
        protected void Page_Load (object sender, EventArgs e)
        {

             Privilegije p = SessionUtils.getCurrentPrivileges();
            
                if (!Page.IsPostBack)
                {
                    if (p.SopsOglasi == 1)
                    {
                        User k = SessionUtils.getCurrentUser();
                        // Label1.Text = k.ToString();
                        IList<Oglas> oglasi = AutomobiliDataManager.instance.dohvatiOglase(k.UserId);

                        GridView1.DataSource = oglasi;

                        GridView1.DataBind();
                    }
                }
            
            //if (!Page.IsPostBack)
            //{
               // buttons();
            //}   
           
         
            //}
        }

        protected void Insert (object sender, GridViewCommandEventArgs e)
        {
              User k = SessionUtils.getCurrentUser();
            int index = Convert.ToInt32(e.CommandArgument);
            int stat = Int32.Parse(GridView1.Rows[index].Cells[6].Text);
            GridViewRow selectedRow = GridView1.Rows[index];
         
            Button b = (Button)selectedRow.Cells[7].Controls[0];
            int idOgl = Int32.Parse(GridView1.Rows[index].Cells[0].Text);
          
            if (stat == 0)
            {
                
                
                AutomobiliDataManager.instance.aktivacija(idOgl, stat);
             //   b.Text = "Deaktiviraj";
                
            }
            else if (stat == 1)
            {
                
                AutomobiliDataManager.instance.aktivacija(idOgl, stat);
               // b.Text = "Aktiviraj";
         
            }
            IList<Oglas> oglasi = AutomobiliDataManager.instance.dohvatiOglase(k.UserId);

            GridView1.DataSource = oglasi;
            GridView1.DataBind();
        }

        //protected void buttons ()
        //{
        //    for (int i = 0; i < GridView1.Rows.Count; i++)
        //    {
        //        GridViewRow row = GridView1.Rows[i];
        //        Button b = (Button)row.Cells[7].Controls[0];
        //        int stat = Int32.Parse(GridView1.Rows[i].Cells[6].Text);
        //        if (stat == 1)
        //        {

        //            b.Text = "Deaktiviraj";
        //        }
        //        else if (stat == 0)
        //        {
        //            b.Text = "Aktiviraj";
        //        }
        //    }
        //}

        protected void Prikaz (object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;

           // DataBind();
        }

        protected void Prikazi (object sender, EventArgs e)
        {
            DataBind();
        }
        

        
    }
}